package net.caixiaomi.info.Lottery.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import net.caixiaomi.info.Lottery.callback.BallCallBack;
import net.caixiaomi.info.Lottery.model.BallItem;
import net.caixiaomi.info.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suntian on 2018/8/15.
 */

public class HorizontalBallView extends LinearLayout implements View.OnClickListener{

    private Context mContext;

    private List<BallItem> mBallList;
    private int mType;  //0标准前区 1标准后区
    private int mCount;
    private int mTextNormalColor;
    private int mBackgroundNormal;
    private int mTextPreColor;
    private int mBackgroundPre;
    public static final int RED_TYPE=0;
    public static final int BLUE_TYPE=1;

    private BallCallBack mCallBack;

    public HorizontalBallView(Context context) {
        super(context);
    }

    public HorizontalBallView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
    }

    public void initData(int count,int colorType,int type,BallCallBack callBack){

        mCount=count;
        mCallBack=callBack;
        mType=type;

        mBallList=new ArrayList<>();
        switch (colorType){
            case RED_TYPE:
                mTextNormalColor=mContext.getResources().getColor(R.color.red);
                mTextPreColor=mContext.getResources().getColor(R.color.white);
                mBackgroundNormal=R.drawable.ball_white_circle;
                mBackgroundPre=R.drawable.ball_red_circle;
                break;
            case BLUE_TYPE:
                mTextNormalColor=mContext.getResources().getColor(R.color.blue);
                mTextPreColor=mContext.getResources().getColor(R.color.white);
                mBackgroundNormal=R.drawable.ball_white_circle;
                mBackgroundPre=R.drawable.ball_blue_circle;
                break;
        }
        for(int i=1;i<mCount+1;i++){
            View view=View.inflate(mContext, R.layout.ball_item_view,null);
            RelativeLayout lGroup=view.findViewById(R.id.lottery_easy_ball_group);
            lGroup.setOnClickListener(this);
            lGroup.setBackgroundResource(mBackgroundNormal);
            lGroup.setTag(i);
            TextView tv=view.findViewById(R.id.lottery_easy_ball_tv);
            tv.setText(i<10?"0"+i:""+i);
            tv.setTextColor(mTextNormalColor);
            addView(view);
            BallItem item=new BallItem();
            item.setSelect(false);
            item.setNum(i);
            mBallList.add(item);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.lottery_easy_ball_group:
                TextView tv=view.findViewById(R.id.lottery_easy_ball_tv);
                int tag= (int) view.getTag();
                BallItem ballItem=mBallList.get(tag-1);

                boolean select=ballItem.isSelect();
                ballItem.setSelect(!select);
                //-------
                if(mCallBack.gonChangeBallData(mType,ballItem)){

                    setBallStatus();
//                    if(ballItem.isSelect()){
//                        view.setBackgroundResource(mBackgroundNormal);
//                        tv.setTextColor(mTextNormalColor);
//                        ballItem.setSelect(false);
//                    }else{
//                        view.setBackgroundResource(mBackgroundPre);
//                        tv.setTextColor(mTextPreColor);
//                        ballItem.setSelect(true);
//                    }

                }else{
                    ballItem.setSelect(select);
                }

//                if(ballItem.isSelect()){
//                    view.setBackgroundResource(mBackgroundNormal);
//                    tv.setTextColor(mTextNormalColor);
//                    ballItem.setSelect(false);
//                }else{
//                    view.setBackgroundResource(mBackgroundPre);
//                    tv.setTextColor(mTextPreColor);
//                    ballItem.setSelect(true);
//                }
                break;
        }
    }

    public List<BallItem> getBallData(){
        return mBallList;
    }

    public void clearAll(){
        for(int i=0;i<mBallList.size();i++){
            mBallList.get(i).setSelect(false);
        }
        setBallStatus();
    }

    public void setBallStatus(){
        for(int i=0;i<mBallList.size();i++){
            View item=getChildAt(i);
            RelativeLayout lGroup=item.findViewById(R.id.lottery_easy_ball_group);
            TextView tv=item.findViewById(R.id.lottery_easy_ball_tv);
            if(mBallList.get(i).isSelect()){
                lGroup.setBackgroundResource(mBackgroundPre);
                tv.setTextColor(mTextPreColor);
            }else{
                lGroup.setBackgroundResource(mBackgroundNormal);
                tv.setTextColor(mTextNormalColor);
            }
        }
    }

    /**
     * 设置选中球
     * @param list
     * @param clear
     */
//    public void setCheckData(List<BallItem> list,boolean clear){
//        if(clear) {
//            clearAll();
//        }
//        if(list!=null) {
//            for (int i = 0; i < list.size(); i++) {
//                for (int j = 0; j < mBallList.size(); j++) {
//                    BallItem ballItem = mBallList.get(j);
//                    BallItem item = list.get(i);
//                    if (ballItem.getNum() == item.getNum()) {
//                        ballItem.setSelect(item.isSelect());
//                    }
//                }
//            }
//        }
//
//        mCallBack.gonChangeBallData(mType,null);
//        setBallStatus();
//    }
}
